<template>
  <div class="login-container">
    <h1>用户登录</h1>
    <el-form 
      :model="form" 
      :rules="rules" 
      ref="formRef" 
      label-width="80px"
      class="login-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button 
          type="primary" 
          :loading="isLoading" 
          @click="handleLogin"
          class="login-btn"
        >
          {{ isLoading ? '登录中...' : '登录' }}
        </el-button>
        <el-button @click="goToRegister">注册新账号</el-button>
      </el-form-item>
    </el-form>

    <!-- 登录成功提示（带动画） -->
    <div 
      v-show="loginSuccess" 
      class="success-toast" 
      @click="hideToast"
    >
      <i class="el-icon-check success-icon"></i>
      <div class="toast-content">
        <p>登录成功！</p>
        <p v-if="form.username">欢迎你，{{ form.username }}！</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/userStore';
import { ElMessage } from 'element-plus'; // 引入消息提示

const router = useRouter();
const userStore = useUserStore();
const formRef = ref(null);

// 登录状态
const isLoading = ref(false); // 登录加载状态
const loginSuccess = ref(false); // 登录成功状态

// 表单数据
const form = reactive({
  username: '',
  password: ''
});

// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
});

// 登录处理
const handleLogin = async () => {
  try {
    isLoading.value = true; // 显示加载状态
    await formRef.value.validate(); // 表单验证

    // 调用登录逻辑（模拟异步请求，实际需替换为真实接口）
    await new Promise(resolve => setTimeout(resolve, 800)); 
    await userStore.login(form); // 调用 store 中的登录方法

    // 登录成功提示
    loginSuccess.value = true; // 显示成功提示
    ElMessage.success('登录成功，正在跳转...'); // 顶部消息提示
    setTimeout(() => {
      router.push('/user'); // 延迟跳转（避免动画被打断）
      loginSuccess.value = false; // 隐藏提示
    }, 1500);
  } catch (error) {
    ElMessage.error(error.message || '登录失败，请检查账号密码');
  } finally {
    isLoading.value = false; // 关闭加载状态
  }
};

// 跳转到注册页
const goToRegister = () => {
  router.push('/register');
};

// 点击提示框隐藏动画
const hideToast = () => {
  loginSuccess.value = false;
};

// 页面加载时隐藏提示（防止刷新残留）
onMounted(() => {
  loginSuccess.value = false;
});
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  position: relative; /* 用于定位提示框 */
}

.login-form {
  margin-top: 20px;
  background: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.login-btn {
  width: 100%;
  margin-right: 10px;
}

/* 登录成功提示样式 */
.success-toast {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: #f0f9eb;
  color: #67c23a;
  padding: 20px 30px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(103, 194, 58, 0.2);
  transition: opacity 0.3s ease;
  opacity: 1;
  pointer-events: auto; /* 允许点击 */
}

.success-toast:hover {
  box-shadow: 0 8px 30px rgba(103, 194, 58, 0.3);
}

.success-icon {
  font-size: 36px;
  margin-right: 15px;
  vertical-align: middle;
  animation: bounce 0.8s ease infinite; /* 呼吸动画 */
}

.toast-content {
  display: inline-block;
  vertical-align: middle;
}

/* 点击隐藏时的动画 */
.success-toast[aria-hidden="true"] {
  opacity: 0;
  pointer-events: none; /* 隐藏时禁止点击 */
}

@keyframes bounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}
</style>